package org.linuxgears.BouncingBall.tools;

import java.awt.Point;
import java.awt.geom.Point2D;

import org.linuxgears.BouncingBall.model.Ball;

public class BallTools {
    public static double distance(Point a, Point b) {
    	return Point2D.distance(a.x, a.y, b.x, b.y);
    };    	
    
    public static double distanceSq(Point a, Point b) {
    	return Point2D.distanceSq(a.x, a.y, b.x, b.y);
    };    	
    
    public static Point getBallCenterByPosition(Ball ball, Point position) {
    	int radius = ball.getSize() / 2;
    	
    	return new Point(position.x + radius, position.y + radius);
    }

	public static Point getBallPositionByCenter(Ball ball, Point position) {
		int radius = ball.getSize() / 2;
		// TODO Auto-generated method stub
		return new Point(position.x - radius, position.y - radius);
	}
	
	public static Point expandVector(Point thisPoint, Point thatPoint, double absolute) {
		// calculate velocity vector based on the default ball velocity
		// and target position

		// speedFactor is distance to go divided by distance
		// moved in one tick, IOW "velocity"
		double distanceX = thatPoint.x - thisPoint.x;
		double distanceY = thatPoint.y - thisPoint.y;
		double distanceToTarget = Math.sqrt(Math.pow(distanceX, 2)
				+ Math.pow(distanceY, 2));

		Point velocity = new Point();
		velocity.x = Math
				.round((float) (distanceX * (absolute / distanceToTarget)));
		velocity.y = Math
				.round((float) (distanceY * (absolute / distanceToTarget)));
		
		return velocity;
	}
	
}